package cn.itcast.service.impl;

import cn.itcast.common.DeleteException;
import cn.itcast.common.R;
import cn.itcast.dto.DishDto;
import cn.itcast.entity.Category;
import cn.itcast.entity.Dish;
import cn.itcast.entity.DishFlavor;
import cn.itcast.mapper.DishMapper;
import cn.itcast.service.CategoryService;
import cn.itcast.service.DishFlavorService;
import cn.itcast.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品 包括口味
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(@RequestBody DishDto dishDto){
        this.save(dishDto);

        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(collect);
    }

    /**
     * 修改时数据的回显
     * @param id
     * @return
     */
    @Override
    public DishDto reSee(@PathVariable long id) {
        Dish byId = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId,dishDto);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<DishFlavor>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,byId.getId());
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(list);

        return dishDto;


    }

    /**
     * 修改菜品包括口味
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);


        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);


    }

    /**
     * 在售的不可以删除,禁售的可以删除
     * @param ids
     */
    @Override
    public void deleteWithFlavor(List<Long> ids) {
       LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
 /*        for (Long id : ids) {
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        }*/

       LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);
        for (Dish dish : list) {
          if( dish.getStatus() == 1){
              throw new DeleteException("在售的菜品不能删除");
          }else {
              dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
              dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
              dishService.removeById(dish.getId());
          }
        }


    }


}
